package com.jams.goodox.activity.serve.headhunter;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.constant.AppConfig;
import com.jams.goodox.entity.ChooseItem;
import com.jams.goodox.entity.JobBean;
import com.jams.goodox.entity.resp.UserJobListResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.HeadHunterService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DelayUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.widget.RefreshRecyclerView;
import com.jams.goodox.widget.mannger.RefreshLinearLayoutManager;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;
import com.jams.library.listener.OnRecycleItemClickListener;
import com.jams.library.widget.RecyclerDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * 人人都是猎头 职位列表
 * <p>
 * create by 顾佳佳 on 2017/05/16
 */
public class JobListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.rcv_job)
    RefreshRecyclerView rcvJob;
    @Bind(R.id.swf_job)
    SwipeRefreshLayout swfJob;
    @Bind(R.id.empty_view)
    LoadView emptyView;

    private List<JobBean> dataList;
    private int curPage;
    private List<ChooseItem> yearList, educationList, priseList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_job_list;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "职位列表");
        swfJob.setOnRefreshListener(this);
        rcvJob.setLayoutManager(new RefreshLinearLayoutManager(this));
        dataList = new ArrayList<>();
        yearList = CollectionUtils.arrayToList(getResources().getStringArray(R.array.year_need), -1);
        educationList = CollectionUtils.arrayToList(getResources().getStringArray(R.array.education_need), -1);
        priseList = CollectionUtils.arrayToList(getResources().getStringArray(R.array.prise_need), -1);
        rcvJob.setAdapter(new RecycleCommonAdapter<JobBean>(this, dataList, R.layout.job_list_item) {
            @Override
            public void convert(RecycleCommonViewHolder helper, final JobBean item, int position) {
                helper.setSimpleDraweeView(R.id.sdv_job,item.getEnterpriseLogo())
                        .setText(R.id.tv_job_name,item.getJobName())
                        .setText(R.id.tv_job_price,priseList.get(item.getSalary()).getName())
                        .setText(R.id.tv_job_company,item.getEnterpriseName())
                        .setText(R.id.tv_job_need,yearList.get(item.getJobLifeRequire()).getName()+" "+educationList.get(item.getEducationRequire()).getName())
                        .setText(R.id.tv_recommend_money, StringUtils.doubleToIntegerString(item.getReward())+"元")
                        .setText(R.id.tv_recommend_time,item.getCreateTime())
                        .setOnItemClickListener(new OnRecycleItemClickListener() {
                    @Override
                    public void itemClick() {
                    }
                });

            }
        });

        rcvJob.addItemDecoration(new RecyclerDivider(this, LinearLayoutManager.VERTICAL, 2, ContextCompat.getColor(this, R.color.gray_background)));
        rcvJob.setLoadDataListener(new RefreshRecyclerView.LoadDataListener() {
            @Override
            public void onLoadMore() {
                getJobList();
            }
        });
        onRefresh();
    }

    @OnClick(R.id.fl_search)
    public void onViewClicked() {
        qStartActivity(JobListSearchActivity.class);
    }

    @Override
    public void onRefresh() {
        DelayUtils.doAfterDelay(100, new DelayUtils.DelayDoListener() {
            @Override
            public void doAction() {
                swfJob.setRefreshing(true);
                curPage = 1;
                getJobList();
            }
        });
    }

    //获取职位列表
    private void getJobList() {
        enqueue(createCallService(HeadHunterService.class).getUserJobList(null,curPage, AppConfig.PagSize), new RetrofitCallbackListener<UserJobListResp>() {
            @Override
            public void onSuccess(Call<UserJobListResp> call, UserJobListResp result) {
                swfJob.setRefreshing(false);
                rcvJob.loadMoreComplete();
                if (result.getData() != null && result.getData().getResultList() != null) {
                    if (curPage == 1) {
                        dataList.clear();
                    }
                    if (result.getData().getPageInfo() == null) {
                        rcvJob.setCanLoadMore(false);
                    } else {
                        rcvJob.setCanLoadMore(result.getData().getPageInfo().isHasMore());
                    }
                    dataList.addAll(result.getData().getResultList());
                    curPage++;
                    rcvJob.notifyData();
                }
                if (!CollectionUtils.hasData(dataList)) {
                    emptyView.loadCompleteNoData(R.mipmap.holder_no_job,"暂无职位");
                } else {
                    emptyView.loadComplete();
                }
            }

            @Override
            public void onError(String msg) {
                swfJob.setRefreshing(false);
                rcvJob.loadMoreComplete();
                if (!CollectionUtils.hasData(dataList)) {
                    emptyView.loadCompleteNoData(R.mipmap.holder_no_job,"暂无职位");
                } else {
                    emptyView.loadComplete();
                }
            }
        });
    }
}
